package jbrowser.gui.settings;

import java.util.Properties;
import javax.swing.JComponent;
import jbrowser.services.ConfigurationService;

/**
 *
 * @author buczko
 */
class UserObject {

    private Properties properties;
    private String name;
    private JComponent editor;
    private ConfigurationService service;

    public UserObject(String name, ConfigurationService service) {
        this.name = name;
        this.service = service;
    }

    @Override
    public String toString() {
        return name.substring(name.lastIndexOf(".") + 1);
    }

    public Properties getProperties() {
        if (properties == null) {
            properties = service.getProperties(name);
        }
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public String getName() {
        return name;
    }

    public JComponent getEditor() {
        if (editor == null) {
            String editorClass = getProperties().getProperty("config.editor.class.uri");
            if (editorClass == null || editorClass.equals("jbrowser.gui.settings.DefaultPropertyEditor")) {
                editor = new DefaultPropertyEditor(getProperties());
            } else {
                // load class
                editor = new DefaultPropertyEditor(getProperties());
            }
        }
        return editor;
    }

    public void setEditor(JComponent editor) {
        this.editor = editor;
    }
}
